package cz.data.domain.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cz.data.domain.system.dao.AuthorizeClientDao;
import cz.data.domain.system.mapstruct.AuthorizeClientMapper;
import cz.data.domain.system.model.dto.AuthorizeClientDto;
import cz.data.domain.system.model.entity.AuthorizeClient;
import cz.data.domain.system.model.vo.AuthorizeClientVo;
import cz.data.domain.system.service.AuthorizeClientService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthorizeClientServiceImpl extends ServiceImpl<AuthorizeClientDao, AuthorizeClient>
        implements AuthorizeClientService {

    @Resource
    AuthorizeClientMapper mapper;
    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public AuthorizeClientVo getAuthorizeClientByClientKey(String clientKey) {
        QueryWrapper<AuthorizeClient> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AuthorizeClient::getClientKey, clientKey);
        return mapper.toVO(getOne(wrapper));
    }

    @Override
    public AuthorizeClient saveAuthorizeClient(AuthorizeClientDto request) {
        AuthorizeClient authorizeClient = mapper.toEntity(request);
        save(authorizeClient);
        return authorizeClient;
    }

    @Override
    public AuthorizeClient updateAuthorizeClient(AuthorizeClientDto request) {
        AuthorizeClient authorizeClient = mapper.toEntity(request);
        updateById(authorizeClient);
        return authorizeClient;
    }
}
